/**
 * Copyright 2012 Facebook
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.emath;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.json.JSONException;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.emath.ConnecttoPlayer.Connecting;
import com.emath.activities.GameMenu;
import com.emath.objects.Username;
import com.emath.shared.Globals;
import com.emath.shared.RestClient;
import com.facebook.FacebookAuthorizationException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.ProfilePictureView;
import com.lnl.R;

public class EnterUsername extends FragmentActivity {

	// private static final List<String> PERMISSIONS = Arrays
	// .asList("publish_actions");
	// private static final Location SEATTLE_LOCATION = new Location("") {
	//
	// {
	// setLatitude(47.6097);
	// setLongitude(-122.3331);
	// }
	// };
	


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		setContentView(R.layout.enterusername);

		final Button ok = (Button) findViewById(R.id.button_ok);
		// connect.setTypeface(tf);
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				EditText username = (EditText) findViewById(R.id.edit_username);
				Globals.collectionObj.setUserID(username.getText().toString());
				Username.setUser(username.getText().toString());
				Globals.user.setUserID(username.getText().toString());
				gomenu();
			}
		});
	}
				

			@Override
			protected void onPostResume() {
				if (Globals.focusedActivity.equals("enter_username")) {
					startService(Globals.backgroundMusicService);
					Globals.isMusicPlaying = true;
				}
				super.onPostResume();
			}

			@Override
			protected void onPause() {
			
				if (this.isFinishing()) {
					Globals.focusedActivity = "NA";
				} else if (Globals.focusedActivity
						.equals("enter_username")) {
					stopService(Globals.backgroundMusicService);
					Globals.isMusicPlaying = false;
				}
				super.onPause();
			}

			@Override
			public void onDestroy() {
				stopService(Globals.backgroundMusicService);
				Globals.isMusicPlaying = false;
				super.onDestroy();
			}

			

			// private interface GraphObjectWithId extends GraphObject {
			// String getId();
			// }

			private void gomenu() {

				new Connecting().execute(getApplicationContext());
			}

		

			public class Connecting extends AsyncTask<Context, Void, Context> {
				ProgressDialog dialog;
				RestClient Connect = new RestClient();
				protected Context doInBackground(Context... Contexts) {
					// while (!Globals.user.getObjectState().equals("free")) {
					// try { // to get tokens
					// if (Globals.user.getObjectState().equals(""))
					// Globals.user.sqlGetUser();
					// Thread.sleep(50);
					// } catch (InterruptedException e) {
					// e.printStackTrace();
					// } catch (Exception e) {
					// e.printStackTrace();
					// }
					// }
					String login = "http://emath-project.appspot.com/emath/emathservicehttp?function=login&userID=";
					login = login + Username.getUser();
					/*
					if (Username.getUser() == null)
						this.cancel(true);
					if (this.isCancelled()) {
						dialog.dismiss();
						return Contexts[0];
					}
					*/
					Connect.connect(login); 
					if (Globals.isconnected== 0){

						Globals.isconnected=1;
						this.cancel(true);

					}
					
					Connect.clear();

					return Contexts[0];
				}

				protected void onPreExecute() {
					Globals.isconnected=1;

					dialog = ProgressDialog.show(EnterUsername.this, "",
							getString(R.string.Loading), true);

				}
				protected void onPostExecute(Context result) {
					if (this.isCancelled()){
						Connect.clear();
						dialog.dismiss();
						Toast.makeText(EnterUsername.this, "Cannot Connect, Check your connection!", Toast.LENGTH_LONG).show();
						return;
					}

					dialog.dismiss();
					Intent intent = new Intent(result, GameMenu.class);
					Globals.focusedActivity = "GameMenu";
					startActivity(intent);
					finish();
				}
			}

		}

